package com.taijihuabao.modules.controller;

import com.github.pagehelper.PageInfo;
import com.taijihuabao.common.exception.ServiceException;
import com.taijihuabao.common.jsonresult.ApplicationResponseCode;
import com.taijihuabao.common.jsonresult.JsonResult;
import com.taijihuabao.common.redisconfig.UserLoad;
import com.taijihuabao.modules.entity.DeptEntity;
import com.taijihuabao.modules.entity.DeptUserEntity;
import com.taijihuabao.modules.entity.UserEntity;
import com.taijihuabao.modules.model.dept.dept.DeptDTO;
import com.taijihuabao.modules.model.dept.dept.DeptPageDTO;
import com.taijihuabao.modules.enums.optenums.dept.DeptEnum;
import com.taijihuabao.modules.service.DeptService;
import com.taijihuabao.modules.service.DeptUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 部门管理
 *
 * @author Xiao
 */
@Api(tags = "部门管理")
@RestController
@RequestMapping("/api/v1/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;
    @Autowired
    private DeptUserService deptUserService;

    /**
     * 获取部门列表(带分页)
     *
     * @param deptDTO
     * @return
     */
    @ApiOperation("获取部门列表(带分页)")
    @GetMapping
    public JsonResult selectAllDept(DeptPageDTO deptDTO) {
        PageInfo pageInfo = deptService.selectAllDept(deptDTO);
        return JsonResult.success(pageInfo);
    }

    /**
     * 获取所有部门
     *
     * @return
     */
    @ApiOperation("获取所有部门")
    @GetMapping("/seletor")
    public JsonResult selectDepts() {
        List<DeptEntity> data = deptService.selectDepts();
        return JsonResult.success(data);
    }

    /**
     * 通过编码获取信息
     *
     * @param deptDTO
     * @return
     */
    @ApiOperation("通过部门编码获取信息")
    @GetMapping("/code")
    public JsonResult selectDeptByCode(DeptPageDTO deptDTO) {
        DeptEntity data = deptService.selectDeptByCode(deptDTO.getDeptCode());
        return JsonResult.success(data);
    }

    /**
     * 通过name获取信息
     *
     * @param deptDTO
     * @return
     */
    @ApiOperation("通过部门名称获取信息")
    @GetMapping("/name")
    public JsonResult selectDeptByName(DeptPageDTO deptDTO) {
        List<DeptEntity> data = deptService.selectDeptByName(deptDTO.getDeptName());
        return JsonResult.success(data);
    }

    /**
     * 通过id获取信息
     *
     * @param deptDTO
     * @return
     */
    @ApiOperation("通过id获取信息")
    @GetMapping("/id")
    public JsonResult selectDeptById(DeptPageDTO deptDTO) {
        DeptEntity data = deptService.selectDeptById(deptDTO.getId());
        return JsonResult.success(data);
    }

    /**
     * 新增部门
     *
     * @param deptEntity
     * @return
     */
    @ApiOperation("新增部门,可以给部门赋部门角色（集合）")
    @PostMapping
    public JsonResult insertDept(@RequestBody DeptDTO deptEntity) {
        UserEntity loginUser = UserLoad.getUser();
        //判断登录用户的身份
        deptJurisdiction(loginUser);
        deptService.insertDept(deptEntity);
        return JsonResult.success(deptEntity);
    }

    /**
     * 修改部门
     *
     * @param deptEntity
     * @return
     */
    @ApiOperation("修改部门")
    @PutMapping
    public JsonResult updateDept(@RequestBody DeptDTO deptEntity) {
        UserEntity loginUser = UserLoad.getUser();
        //判断登录用户的身份
        deptJurisdiction(loginUser);
        deptService.updateDept(deptEntity);
        return JsonResult.success(deptEntity);
    }

    /**
     * 删除部门
     *
     * @param deptEntity
     * @return
     */
    @ApiOperation("删除部门")
    @DeleteMapping
    public JsonResult deleteDept(@RequestBody DeptDTO deptEntity) {
        UserEntity loginUser = UserLoad.getUser();
        //判断登录用户的身份
        deptJurisdiction(loginUser);
        deptService.deleteDept(deptEntity);
        return JsonResult.success(deptEntity.getId());
    }

    /**
     * 判断登录用户的权限是否能够更改部门表
     *@param loginUser
     */
    private void deptJurisdiction(UserEntity loginUser) {
        List<DeptUserEntity> list = deptUserService.selectDeptByUserId(loginUser.getId());
        DeptUserEntity d1 = new DeptUserEntity();
        for (DeptUserEntity deptUserEntity : list) {
            //判断是否为总经理办公室
            if (DeptEnum.GENERALMANAGER.getCode().equals(deptUserEntity.getDeptCode())) {
                d1 = deptUserEntity;
                break;
            }
        }
        if (Objects.isNull(d1.getUserId())) {
            throw new ServiceException(ApplicationResponseCode.DEPT_OUT_BOUNDS);
        }
    }
}
